package org.jeecg.modules.qyxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.hibernate.validator.constraints.NotBlank;
import org.jeecg.modules.qyxx.entity.BgHzNdSbQwsz;
import org.jeecg.modules.qyxx.entity.req.BgNsrjbxxReq;
import org.jeecg.modules.qyxx.mapper.BgHzNdSbQwszMapper;
import org.jeecg.modules.qyxx.service.IBgHzNdSbQwszService;
import org.jeecg.modules.qyxx.service.IBgSjmkjgOrderService;
import org.jeecg.modules.qyxx.util.DateRange;
import org.jeecg.modules.qyxx.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: 前5申报税目数据
 * @Author: jeecg-boot
 * @Date: 2022-05-09
 * @Version: V1.0
 */
@Service
public class BgHzNdSbQwszServiceImpl extends MppServiceImpl<BgHzNdSbQwszMapper, BgHzNdSbQwsz> implements IBgHzNdSbQwszService {
    @Autowired
    private IBgSjmkjgOrderService bgSjmkjgOrderService;

    @Override
    public Map getTop5SzFx(BgNsrjbxxReq req) {
        DateUtils utils = DateUtils.getInstance();
        Date jssj = req.getJssj();
        // String year = bgSjmkjgOrderService.getNewByDjxh(req.getDjxh());
        String year = utils.getYearToStr(jssj);
        Map<String, Object> m = new HashMap<>();
        List<String> tjnds = utils.nearlyThreeYears(req.getKssj(), req.getJssj());
        List<String> zsxmpm = new ArrayList<>();
        // 获取征收税目排名名称
        for (int i = tjnds.size() - 1; i >= 0; i--) {
            String tjnd = tjnds.get(i);
            /*LambdaQueryWrapper<BgHzNdSbQwsz> wrapper = Wrappers.lambdaQuery(BgHzNdSbQwsz.class);
            wrapper.select(BgHzNdSbQwsz::getZsxmmc)
                    .eq(BgHzNdSbQwsz::getDjxh, req.getDjxh())
                    .eq(BgHzNdSbQwsz::getTjnd, tjnd)
                    .orderByDesc(BgHzNdSbQwsz::getTjnd)
                    .orderByAsc(BgHzNdSbQwsz::getPm).last("rownum > 0 and rownum <= 5");

            zsxmpm = listObjs(wrapper, a -> {
                return String.valueOf(a.toString());
            });*/
            zsxmpm = baseMapper.getZsxmmcByTjnd(req.getDjxh(), tjnd);
            if (zsxmpm != null && zsxmpm.size() > 0) {
                // List<String> zsxmmcList = baseMapper.getZsxmmcList(req.getDjxh(), year);
                m.put("zsxmpm", zsxmpm);
                break;
            }
        }


        if (zsxmpm == null || zsxmpm.size() <= 0) {
            return new HashMap<>();
        }

        // 获取征收税目排名数据
        Map<String, Object> map = new LinkedHashMap<>();
        for (String tjnd : tjnds) {
            List<BgHzNdSbQwsz> list = new ArrayList<>();
            for (String zsxmmc : zsxmpm) {
                LambdaQueryWrapper<BgHzNdSbQwsz> query = Wrappers.lambdaQuery(BgHzNdSbQwsz.class);
                query.eq(BgHzNdSbQwsz::getDjxh, req.getDjxh()).eq(BgHzNdSbQwsz::getTjnd, tjnd).eq(BgHzNdSbQwsz::getZsxmmc, zsxmmc);
                BgHzNdSbQwsz qwsz = super.getOne(query);
                if (qwsz != null && qwsz.getYbtse() != null) {
                    qwsz.setYbtse(qwsz.getYbtse().divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP));
                }
                list.add(qwsz);
            }
            //if (list.size() > 0) {
                /*if (year.equals(tjnd)) {
                    String month = bgSjmkjgOrderService.getNewMonthByDjxh(req.getDjxh());
                    String newMonth = month.substring(month.length() - 2);
                    map.put(year + "年(" + newMonth + "月)", list);
                } else {
                    // bgFpHzNdJzdSxyyclcp.setTjnd(year + "年");
                    // map.put("tjnd", year + "年");
                    map.put(tjnd + "年", list);
                }*/
            if (year.equals(tjnd)) {
                DateRange range = utils.getLastQuarter();
                Date end = range.getEnd();
                String dyjd = tjnd + "03";
                String dejd = tjnd + "06";
                String dsjd = tjnd + "09";
                if (dyjd.equals(utils.getMonthToStr(end))) {
                    map.put(tjnd + "年(第一季度)", list);
                } else if (dejd.equals(utils.getMonthToStr(end))) {
                    map.put(tjnd + "年(第二季度)", list);
                } else if (dsjd.equals(utils.getMonthToStr(end))) {
                    map.put(tjnd + "年(第三季度)", list);
                } else {
                    map.put(tjnd + "年", list);
                }
            } else {
                map.put(tjnd + "年", list);
            }
        }
        m.put("data", map);
        m.put("tb", top5SzFxTb(req.getDjxh(), zsxmpm, tjnds));
        return m;
    }

    /**
     * 前五大税种图表
     *
     * @param djxh
     * @param zsxmpm
     * @param tjnds
     * @return
     */
    public List<Map<String, Object>> top5SzFxTb(String djxh, List<String> zsxmpm, List<String> tjnds) {
        List<Map<String, Object>> arrayList = new ArrayList<>();
        for (String tjnd : tjnds) {
            Map<String, Object> map = new HashMap<>();
            List<BigDecimal> list = new ArrayList<>();
            for (String szmc : zsxmpm) {
                LambdaQueryWrapper<BgHzNdSbQwsz> wrapper = Wrappers.lambdaQuery(BgHzNdSbQwsz.class);
                wrapper.select(BgHzNdSbQwsz::getYbtse).eq(BgHzNdSbQwsz::getDjxh, djxh).eq(BgHzNdSbQwsz::getZsxmmc, szmc).eq(BgHzNdSbQwsz::getTjnd, tjnd);
                list.add(getObj(wrapper, a -> {
                    return new BigDecimal(a.toString());
                }));
            }
            map.put("szFxData", list);
            map.put("tjnd", tjnd);
            arrayList.add(map);
        }
        return arrayList;
    }
}
